package com.lishe.lishe.lishe.MyRecyclerView;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextPaint;
import android.view.View;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.utils.Utils;

/**
 * Created by DELL on 2017/10/30.
 */

public class SpaceItemDecoration2 extends RecyclerView.ItemDecoration {
    int mLeftSpace;
    int mRightSpace;
    int mTopSpace;
    int mBottomSpace;
    int mGroupTop;

    private Paint mPaint;
    private TextPaint mTextPaint;
    private int gap;
    private DecorationCallback callback;
    private int typePosition;

    /**
     * @param leftSpace   px
     * @param rightSpace  px
     * @param topSpace    px
     * @param bottomSpace px
     */
    public SpaceItemDecoration2(Context ctx, int leftSpace,
                                int rightSpace,
                                int topSpace,
                                int bottomSpace,
                                int groupTop,
                                DecorationCallback callback) {
        this.callback = callback;

        Resources res = ctx.getResources();
        this.mLeftSpace = leftSpace;
        this.mRightSpace = rightSpace;
        this.mTopSpace = topSpace;
        this.mBottomSpace = bottomSpace;
        this.mGroupTop = groupTop;

        mPaint = new Paint();
        mPaint.setColor(res.getColor(R.color.white));

        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setDither(true);
        mTextPaint.setTextSize(Utils.dip2px(ctx, 14));
        mTextPaint.setColor(res.getColor(R.color.color_333333));
        gap = Utils.dip2px(ctx, 12);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildLayoutPosition(view);
        int type = parent.getAdapter().getItemViewType(position);
        if (type == 1) {
            typePosition=position;
            outRect.top = mGroupTop;
        } else {
            outRect.top = 0;
        }

        int spanCount=getSpanCount(parent);
        if(position-typePosition<=spanCount) {
            if(type!=1)
                outRect.top = mTopSpace;
        }

    }

    /**
     * 获取列数
     *
     * @param parent
     * @return
     */
    private int getSpanCount(RecyclerView parent) {
        // 列数
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {

            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        int left = parent.getPaddingLeft();
        int right = parent.getWidth();
        int childCount = parent.getChildCount();

        int position;
        int type;

        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            position = parent.getChildAdapterPosition(view);
            type = parent.getAdapter().getItemViewType(position);
            if (type == 1) {
                float top = view.getTop() - gap;
                float bottom = view.getBottom();
                c.drawRect(left, view.getTop() - mGroupTop, right, bottom, mPaint);//绘制灰色矩形
                c.drawText(callback.getGroupFirstLine(position), left, top, mTextPaint);//绘制文本

            }
        }
    }

    public interface DecorationCallback {
        String getGroupFirstLine(int position);
    }
}
